package com.jfsoft.demo1114.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.jfsoft.demo1114.dao.model.Studys;
import com.jfsoft.demo1114.service.IPageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;

import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class PageController {

    @Autowired
    private IPageService pageService;

    @RequestMapping("")
    public String toIndex(String pname, String pid, String beginDate, String endDate, Model model) {
        if (!StringUtils.isEmpty(pid)) {
            pid = pid.trim();
        }
        model.addAttribute("pname", pname);
        model.addAttribute("pid", pid);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (StringUtils.isEmpty(beginDate)) {
            Date d = new Date();
            d.setTime(d.getTime() - 7 * 24 * 60 * 60 * 1000l);
            beginDate = sdf.format(d);
        }
        if (StringUtils.isEmpty(endDate)) {
            endDate = sdf.format(new Date());
        }
        model.addAttribute("beginDate", beginDate);
        model.addAttribute("endDate", endDate);

        Map<String, Object> map = new HashMap<>();
        List<Studys> list = new ArrayList<>();
        try {
            if (!StringUtils.isEmpty(pid)) {
                map = pageService.query(pid, null, null, null, null, null
                        , null, null, null, null);
                list = (List<Studys>) map.get("data");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        String html = "";
        for (Studys s : list) {
            html += "<tr>" +
                    "<td><input type='checkbox' name='stu' value='" + s.getStuuid() + "'/></td>" +
                    "<td>" + s.getPid() + "</td>" +
                    "<td>" + s.getStudateStr() + " " + s.getStutimeStr() + "</td>" +
                    "<td>" + s.getPname() + "</td>" +
                    "<td>" + s.getGenderStr() + "</td>" +
                    "<td>" + s.getAgeStr() + "</td>" +
                    "<td>" + s.getStudescStr() + "</td>" +
                    "<td>" + s.getModality() + "</td>" +
                    "<td>" + s.getImagecount() + "</td>" +
                    "</tr>";
            // console.log(data.data[i])
        }
        model.addAttribute("html", html);

        return "list";
    }

    /**
     * @param pid       患者编号
     * @param pname     患者姓名
     * @param ageUnit   年龄单位
     * @param gender    性别
     * @param modality  检查类型
     * @param beginDate 检查日期 开始日期
     * @param endDate   检查日期 结束日期
     * @param studesc   检查部位
     * @return
     */
    @RequestMapping("/query")
    @ResponseBody
    public String query(String pid, String pname, Integer beginAge, Integer endAge, String ageUnit, String gender
            , String modality, String beginDate, String endDate, String studesc) {
        Map<String, Object> map = new HashMap<>();

        try {
            map = pageService.query(pid, pname, beginAge, endAge, ageUnit, gender, modality, beginDate, endDate
                    , studesc);
        } catch (Exception e) {
            e.printStackTrace();
        }

        SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Studys.class,
                "stuuid", "pid", "pname", "genderStr", "ageStr", "modality"
                , "studescStr", "studateStr", "imagecount", "stutimeStr");

        return JSON.toJSONString(map, filter,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullNumberAsZero,
                SerializerFeature.WriteNullStringAsEmpty);
    }
}
